/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "EPost.h"

#include <QtCore/QUrl>
#include <QtCore/QUrlQuery>
#include <QtCore/QFile>

#include "OutputParser.h"

using namespace Bio;

const QString	c_epostUrl = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/epost.fcgi";

///////////////////////////////////////////////////////////////////////////////
//	EPost
///////////////////////////////////////////////////////////////////////////////

/**
	@class EPost
	@brief Implementation of the Entrez 'EPost' function.

	Provides an interface to the NCBI Entrez 'EPost' function.

	See http://www.ncbi.nlm.nih.gov/books/NBK25499/ for details on
	the Entrez EPost utility.
**/

/**
	Default Constructor. Creates a new EPost operation.
**/
EPost::EPost()
{
}

/**
**/
EPost::EPost( const QString &db, const QStringList &ids ) : m_db( db ), m_ids( ids )
{
}

/**
**/
EPost::EPost( const QString &db, const QStringList &ids, const QString &webenv ) : m_db( db ),
	m_ids( ids ), m_webEnv( webenv )
{
}

/**
**/
EPost::~EPost()
{
}

/**
**/
bool EPost::exec()
{
	QUrl			url( c_epostUrl );
	QUrlQuery		urlQuery;
	QFile			output( "EPost.txt" );
	OutputParser	parser( output );

	if( m_db.isEmpty() ){
		error( "No database specified for EPost." );
		return false;
	}

	urlQuery.addQueryItem( "db", m_db );

	if( m_ids.isEmpty() ){
		error( "Must specify IDs for EPost." );
		return false;
	}

	if( !m_webEnv.isEmpty() )
		urlQuery.addQueryItem( "WebEnv", m_webEnv );

	if( m_ids.count() > 200 )
		return execPost( url, m_ids, parser );
		
	urlQuery.addQueryItem( "id", m_ids.join( "," ) );
	
	url.setQuery( urlQuery );

	return execGet( url, parser );
}

/**
**/
void EPost::setDatabase( const QString &db )
{
	m_db = db;
}

/**
**/
QString EPost::getDatabase() const
{
	return m_db;
}

/**
**/
void EPost::setWebEnv( const QString &webEnv )
{
	m_webEnv = webEnv;
}

/**
**/
QString EPost::getWebEnv() const
{
	return m_webEnv;
}

/**
**/
void EPost::addID( const QString &id )
{
	m_ids.append( id );
}

/**
**/
void EPost::setIDs( const QStringList &ids )
{
	m_ids = ids;
}

/**
**/
QStringList EPost::getIDs() const
{
	return m_ids;
}
